from models.users import *
from models import db
from datetime import datetime


# 新闻频道
class Channel(db.Model):
    __tablename__ = 'news_channel_tb'
    cid = db.Column(db.Integer, primary_key=True,autoincrement=True, doc='频道ID')
    cname = db.Column(db.String(32), doc='频道名称')
    ctime = db.Column(db.DateTime, default=datetime.now, doc='创建时间')
    utime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, doc='更新时间')
    sequence = db.Column(db.Integer, default=0, doc='序号')
    is_visible = db.Column(db.Boolean, default=False, doc='是否可见')
    is_default = db.Column(db.Boolean, default=False, doc='是否默认')
    user = db.relationship('User', secondary='user_channel_tb', backref=db.backref('channels'))
    news = db.relationship('News', backref='channel')


# 用户 与 文章表
class Collection(db.Model):
    __tablename__ = 'collection_tb'
    user_id = db.Column(db.Integer, db.ForeignKey('user_basic_tb.uid'), primary_key=True, doc="用户ID")
    news_id = db.Column(db.Integer, db.ForeignKey('news_basic_tb.nid'), primary_key=True, doc="资讯ID")
    is_delete = db.Column(db.Boolean, doc='状态(0,关注;1, 取消)')
    create_time = db.Column(db.DateTime, default=datetime.now, doc='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, doc='更新时间')


# 文章基本信息表
class News(db.Model):
    __tablename__ = 'news_basic_tb'

    class STATUS:
        DRAFT = 0  # 草稿
        UNREVIEWED = 1  # 待审核
        APPROVED = 2  # 审核通过
        FAILED = 3  # 审核失败
        DELETED = 4  # 已删除
        BANNED = 5  # 封禁
    STATUS_ENUM = [0, 1, 2, 3]
    nid = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='文章ID')
    user_id = db.Column(db.Integer, db.ForeignKey('user_basic_tb.uid'), doc='用户ID')
    channel_id = db.Column(db.Integer, db.ForeignKey('news_channel_tb.cid'), doc='频道ID')
    title = db.Column(db.String(640), doc='标题')
    cover = db.Column(db.String(2000), doc='封面')
    is_advertising = db.Column(db.Boolean, default=False, doc='是否投放广告')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, doc='创建时间')
    status = db.Column(db.Integer, default=0, doc='帖文状态')
    reviewer_id = db.Column(db.Integer, doc='审核人员ID')
    review_time = db.Column(db.DateTime, doc='审核时间')
    delete_time = db.Column(db.DateTime, doc='删除时间')
    comment_count = db.Column(db.Integer, default=0, doc='评论数')
    good_count = db.Column(db.Integer, default=0, doc='点赞数')
    allow_comment = db.Column(db.Boolean, default=True, doc='是否允许评论')
    reject_reason = db.Column(db.String(640), doc='驳回原因')
    utime = db.Column(db.DateTime, default=datetime.now, doc='更新时间')
    content = db.Column(db.Text, doc='帖文内容')

    comment = db.relationship('Comment', backref=db.backref('article'))
    user = db.relationship('User', secondary='collection_tb', backref=db.backref('collections'))


# 文章评论
class Comment(db.Model):
    __tablename__ = 'news_comment_tb'
    class STATUS:
        UNREVIEWED = 0  # 待审核
        APPROVED = 1  # 审核通过
        FAILED = 2  # 审核失败
        DELETED = 3  # 已删除

    cmid = db.Column(db.Integer, primary_key=True,autoincrement=True, doc='评论ID')
    user_id = db.Column(db.Integer, db.ForeignKey('user_basic_tb.uid'), doc='用户ID')
    article_id = db.Column(db.Integer, db.ForeignKey('news_basic_tb.nid'), doc='文章ID')
    parent_id = db.Column(db.Integer, db.ForeignKey('news_comment_tb.cmid'), doc='被评论的评论id')
    like_count = db.Column(db.Integer, default=0, doc='点赞数')
    reply_count = db.Column(db.Integer, default=0, doc='回复数')
    content = db.Column(db.String(128), doc='评论内容')
    is_top = db.Column(db.Boolean, default=False, doc='是否置顶')
    status = db.Column(db.Integer, default=1, doc='评论状态')
    ctime = db.Column(db.DateTime, default=datetime.now, doc='创建时间')


# 评论点赞
class CommentLiking(db.Model):
    __tablename__ = 'news_comment_liking_tb'
    liking_id = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='主键ID')
    user_id = db.Column(db.Integer, doc='用户ID')
    comment_id = db.Column(db.Integer, doc='评论ID')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, doc='创建时间')
    is_deleted = db.Column(db.Boolean, default=False, doc='是否删除')


class SearchHistory(db.Model):
    __tablename__ = 'search_history_tb'
    sid = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='搜索记录ID')
    content = db.Column(db.String(128), doc="用户搜索内容")
    user_id = db.Column(db.Integer, db.ForeignKey('user_basic_tb.uid'), doc="用户ID")
